<?php
namespace app\common\lib\module_config\tagsconfig;
use app\common\lib\module_config\TagsConfig;
use app\common\lib\module_config\tagsconfig\Search;
use think\Db;
use app\common\lib\model_operate\Tools;

/**
 * table标签类
 */
class Table extends TagsConfig
{

	private $where ; // 根据search 参数规则生成的查询条件

	private $displayFields; //显示字段 [ 'id'=>'ID' , 'name' => '会员名' ];

	private $relevance; //关联信息

	function __construct( $trace , $tags )
	{
		parent::__construct( $trace , $tags );
		//设置显示字段信息
		$this->setDisplayFields();
		//解析配置信息
		$this->analyticConfig();
	}


	/**
	 * 设置动态参数
	 * @param $searchObj Search标签类对象 
	 */
	public function setParam( $search = null )
	{
		if( !empty( $search ) && $search instanceof Search ){
			$rule = $search->getRule();
			$params = $search->getParam();

			//解析规则,获取搜索where
			$where = [];
			if( !empty( $rule )){
				foreach ( $rule as $index => $rule ) {
					$where [] = $this->replaceRule( $params , $rule , $params [ $index ] ); 
				}
				$this->where = implode( ' AND ', array_filter( $where ) );
			}
		}else{
			throw new \Exception("Table标签依赖Search对象");
			
		}
	}

	/**
	 * 替换规则
	 */
	private function replaceRule( $params , $rule , $selfValue )
	{
		$search = [ '$val' , ' ' ];
		$replace = [ $selfValue , '' ];
		foreach ( $params as $key => $value ) {
			$search [] = '$'.$key; 
			if( is_string( $value ) ){
				$value = "'".$value."'";
			}
			$replace [] = $value;
		}

		$tagKeys 	= array_keys( $this->getTagMap() );
		$tagValue 	= array_values( $this->getTagMap() );

		$search 	= array_merge( $search , $tagKeys );
		$replace 	= array_merge( $replace , $tagValue );

		$rule 		= str_replace( $search , $replace , $rule );
		return $rule;
	}

	/**
	 * 获取搜索参数的条件
	 */
	public function getWhere()
	{
		return $this->where;
	}

	/**
	 * 配置解析
	 */
	private function analyticConfig()
	{
		//解析关联信息
		if( isset( $this->config [ 'relevance' ] ) && !empty( $this->config [ 'relevance' ] ) ){
			$relevance = $this->config [ 'relevance' ] ;
			//主表名
			$this->relevance[ 'baseTableName' ] = Tools::tableNameFormat( $this->trace->getApp() , 1 );
			$this->relevance [ 'relevanceTables' ] = [];

			
			//关联信息
			foreach ( $relevance as $key => $items ) {
				//关联信息临时容器
				$relevanceTmp = [];
				if( empty( $items ) ) continue;
				foreach ( $items as $value ) {
					if( empty( $value ) || count( $value ) != 4  ) continue;
					list( $mainTable , $relevanceTable , $condition , $displayFields ) = $value;
					$relevanceTmp [ 0 ] = $relevanceTable;
					$relevanceTmp [ 1 ] = $condition;
					//追加关联信息中要显示的字段
					$displayStr = '';
					if( !empty( $displayFields ) && is_array( $displayFields ) ){
						foreach ( $displayFields as $fieldName => $aliasName ) {
							$name 	= empty( $aliasName ) ? $fieldName : $aliasName;
							//只要fields中存在该字段就显示,不管是否设置is_show=1
							$title 	= isset( $this->fields [ $name ] ) ? $this->fields [ $name ][ 'title' ] : $name;
							$this->displayFields [ $name ] = $title; 
							//关联查询时设置的显示字段
							$relevanceTmp [ 2 ][ $aliasName ] = $relevanceTable.'.'.$fieldName.' as '.$aliasName ;
						}
					}
				}	
				if( !empty( $relevanceTmp ) && count( $relevanceTmp ) == 3 ){
					$this->relevance [ 'relevanceTables' ] [] = $relevanceTmp ;
				}
			}

		}
	}


	/**
	 *设置要显示的字段信息
	 */
	private function setDisplayFields()
	{
		if( !empty( $this->fields ) ){
			foreach ( $this->fields as $key => $value ) {
				if( $value [ 'is_show' ] = 1 ){
					$index = empty( $value [ 'alias' ]  ) ? $key : $value [ 'alias' ] ;
					$this->displayFields [ $index ] = $value [ 'title' ];
				}
			}
		}
	}

	/**
	 * 获取要显示的字段信息
	 */
	public function getDisplayFields()
	{
		return $this->displayFields;
	}

	/**
	 * 获取关联信息
	 */
	public function getRelevance()
	{
		return $this->relevance;
	}

/*****************************/


	public function createQuery(){
		return '子类 Table 实现的获取数据方法';
	}
	public function createHtml($value = null){
		// dump($this->config);
		// $list = db('member')->select()->toArray();
		// dump($list);
		$html = '';
		//参数的遍历
		// foreach ($list as $key => $value) {
			// $html .= '<tr>';
			//单选框
			// $html .= "<th><input type='checkbox' class='check-box' name='id[]' value='".$value['id']."'></th>";
		if(empty($this->fields)){
			return $html;
		}
		foreach ($this->fields as $k => $v) {
			if($v['show_type'] == '0'){
				continue;
			}
			if(!empty($value[$k])){
				$html .= '<td>'.$value[$k].'</td>';
			}else{
				$html .= '<td></td>';
			}
		}
			// $html .= "<td width='100px'><div class='layui-btn-group'><button type='button' class='layui-btn layui-btn-xs layui-btn-primary update-btn' style='font-weight:800 ;' data-id='".$value['id']."' title='编辑'><i class='layui-icon'>&#xe642;</i></button><button type='button' class='layui-btn layui-btn-xs layui-btn-primary delete-btn' style='font-weight:800 ;' data-id='".$value['id']."' title='删除'><i class='layui-icon'>&#xe640;</i></button><button type='button' class='layui-btn layui-btn-xs layui-btn-primary detail-btn' style='font-weight:800 ;' data-id='".$value['id']."' title='查看'><i class='layui-icon'>&#xe602;</i></button></div></td>";
			// $html .= '</tr>';
		// }
		return $html;
	}

	public function getList(){
		$list = db('member')->select()->toArray();
		return $list;
	}

	//表格头
	public function getHeader(){
		$html = '';
		// $html .= '<thead>';
		// $html .= '<tr>';
		// $html .= "<th width='20px'><input type='checkbox' id='check-box-all' class='layui-form-checkbox'></th>";
		// $html .= "</th>";
		if(empty($this->fields)){
			return $html;
		}
		foreach ($this->fields as $key => $value) {
			if($value['is_show'] == 0){
				continue;
			}
			$html .= '<th>';
			$html .= $value['title'];
			$html .= '</th>';
		}
		// $html .= "<th>操作</th>";
		// $html .= '</tr>';
		// $html .= '</thead>';
		return $html;
	}

	public function getConfig(){
		return $this->config;
	}

}